/**
 * 20170413 bqb
 */
var queryData = {};
var urlDept = window.rootPath + '/ui/CustomerTotalInfo/getCustomerDeptChart';
var urlArea = window.rootPath + '/ui/CustomerTotalInfo/getCustomerAreaChart';
var urlIndustry = window.rootPath + '/ui/CustomerTotalInfo/getCustomerbisChart';
var urlMyTrend = window.rootPath + '/ui/CustomerTotalInfo/getCustomerXYChart';
var urlMyDeptTrend = window.rootPath + '/ui/CustomerTotalInfo/getTrendChart';
var urlMyZCXZTrend = window.rootPath + '/ui/CustomerTotalInfo/getZCXZChart';
/*var urlContractType = window.rootPath + '/ui/contractTotalInfo/getContractTypeChart';
var urlsum = window.rootPath+ '/ui/contractviewindex/getPageSum?type=1';*/
var myDeptChart;
var myAreaChart;
var myIndustryChart;
var myTrendChart;
var myContractTypeChart;


var nowSelectDept;
jQuery(function() {	
	//查询按钮
	initQueryParam();
	jQuery("#queryBtn").on('click',function(){
		queryAllChart();
	})
	//还原按钮
	jQuery("#datehy").on('click',function(){
		var mydate = new Date();
		var begindate = mydate.getFullYear()+"-01-01"
		var enddate = "" + mydate.getFullYear() + "-";
		enddate += ((mydate.getMonth()+1)<10 ? '0'+(mydate.getMonth()+1) : (mydate.getMonth()+1)) + "-";
		enddate += (mydate.getDate()<10 ? '0'+mydate.getDate() : mydate.getDate());
		jQuery("#beginDate").val(begindate);
		$('#endDate').val(getDay(1));
		
		
		queryAllChart();
	})
    
	myDeptChart = echarts.init(document.getElementById('deptDiv'));//部门柱状图
	myAreaChart = echarts.init(document.getElementById('areaDiv'));//片区饼图
	myIndustryChart = echarts.init(document.getElementById('industryDiv'));//行业饼图
	myTrendChart = echarts.init(document.getElementById('trendDiv'));//信用评分
	myTrendDeptChart = echarts.init(document.getElementById('qsfx'));//趋势图
	ZCXZ = echarts.init(document.getElementById('customerZCXZ'));//资产性质
	/*myContractTypeChart = echarts.init(document.getElementById('contractTypeDiv'));//合同类型饼图
*/	
	//资产性质
	ZCXZ.setOption({
        title: {
            text: '资产性质'
        },
        tooltip: {},
        legend: {
            data:['客户数量(个)']
        },
        xAxis: {
            data: []
        },
        yAxis: {
        	 
        },
        itemStyle : {
        	normal: {
        		color: function(params) {
                    var colorList = [
                       '#FF3333','#00c0ef'
                       ];
                    return colorList[params.dataIndex%2]
                },
        	}
        },
        series: [{
            name: '客户数量(个)',
            type: 'bar',
            barWidth : 50,
            data: [],
            itemStyle: {  
                normal: {  
                    label: {  
                        show: true,//是否展示  
                        position: 'top',
                        textStyle: {  
                            fontWeight:'bolder',  
                            fontSize : '12',  
                            fontFamily : '微软雅黑',  
                        }  
                    }  
                }  
            },
        }]
    });
	ZCXZ.on('click',function(params){
    	openContractList(9,params.name);
    });
	//趋势分析
	myTrendDeptChart.setOption({
        title : {
            text: '趋势分析'
        },
        tooltip : {
            trigger: 'axis'
        },
        legend: {
            data:['客户数量'],
            itemWidth:40,
            itemHeight:20,
            textStyle:{
                fontSize:15,
                color:'#fff'
            }
	        /*show:true,
	        orient:'horizontal',
	        borderColor:'#df3434',
	        borderWidth:2,
	        data:[
	            {
	                name:'蒸发量',
	                textStyle:{
	                    fontSize:12,
	                    fontWeight:'bolder',
	                    color:'#cccccc'
	                },
	                icon:'stack'
	            },
	            {
	                name:'降水量',
	                textStyle:{
	                    fontSize:12,
	                    fontWeight:'bolder',
	                    color:'#df3434'
	                },
	                icon:'pie'
	            }
	            ]*/
        },
        toolbox: {
            show : true,
        },
        calculable : true,
        xAxis : [
            {
                type : 'category',
                boundaryGap : false,
                data : []
            }
        ],
        yAxis : [
            {
                type : 'value',
                axisLabel : {
                    formatter: '{value}'
                }
            }
        ],
        series : [
            {
                name:'销售处客户数量',
                type:'line',
                data:[],
                markLine : {
                    data : [
                        {type : 'average', name: '平均值'}
                    ]
                }
            },{
                name:'成套处客户数量',
                type:'line',
                data:[],
                markLine : {
                    data : [
                        {type : 'average', name: '平均值'}
                    ]
                }
            },{
                name:'外贸处客户数量',
                type:'line',
                data:[],
                markLine : {
                    data : [
                        {type : 'average', name: '平均值'}
                    ]
                }
            }
        ]
    });
	myTrendDeptChart.on('click',function(params){
    	var a = params.name;
    	openContractList(8,params.name);
    });
	//部分分析
    myDeptChart.setOption({
        title: {
            text: '部门分析'
        },
        tooltip: {},
        legend: {
            data:['客户数量(个)']
        },
        xAxis: {
            data: []
        },
        yAxis: {
        	 
        },
        itemStyle : {
        	normal: {
        		color: function(params) {
                    var colorList = [
                       '#FF3333','#00c0ef'
                       ];
                    return colorList[params.dataIndex%2]
                },
        	}
        },
        series: [{
            name: '客户数量(个)',
            type: 'bar',
            barWidth : 50,
            data: [],
            itemStyle: {  
                normal: {  
                    label: {  
                        show: true,//是否展示  
                        position: 'top',
                        textStyle: {  
                            fontWeight:'bolder',  
                            fontSize : '12',  
                            fontFamily : '微软雅黑',  
                        }  
                    }  
                }  
            },
        }]
    });
    
    //部门图表单击
    myDeptChart.on('click', function (params) {
    	queryData.beginDate = jQuery('#beginDate').val();
    	queryData.endDate = jQuery('#endDate').val();
    	//queryData.dateTimeType = jQuery('#dateTimeType').val();
    	queryData.deptName = params.name;
    	nowSelectDept = params.name;
    	jQuery.ajax({
			url : encodeURI(urlArea),
			type : 'get',
			data : queryData,
			dataType : 'json',
			contentType : 'application/json;charset=utf-8',
			success : function(data) {
				// 指定图表的配置项和数据
				myAreaChart.setOption({
			        series: [{
		              name: '客户数',
		              type: 'pie',
		              radius: '55%',
		              data:data.data
			        }]
			    });
			}
		});
    });
    
    //双击部门图表打开
    myDeptChart.on('dblclick',function(params){
    	openContractList(1,params.name);
    });
    
    //片区分析
    myAreaChart.setOption({
        title: {
            text: '片区分析'
        },
        tooltip: {},
        series : [{
              name: '客户数量',
              type: 'pie',
              radius: '55%',
              data:[],
              itemStyle:{ 
                  normal:{ 
                        label:{ 
                          show: true, 
                          formatter: '{b} : {c} ({d}%)' 
                        }, 
                        labelLine :{show:true} 
                      } 
                  } 
        }]
    });
   //行业分析
    myIndustryChart.setOption({
        title: {
            text: '行业分析'
        },
        tooltip: {},
        series : [{
              name: '客户数量',
              type: 'pie',
              radius: '55%',
              data:[],
              itemStyle:{ 
                  normal:{ 
                        label:{ 
                          show: true, 
                          formatter: '{b} : {c} ({d}%)' 
                        }, 
                        labelLine :{show:true} 
                      } 
                  } 
        }]
    });
    //信用评分
    myTrendChart.setOption({
        title: {
            text: '信用评分'
        },
        tooltip: {},
        series : [{
              name: '客户数量',
              type: 'pie',
              radius: '55%',
              data:[],
              itemStyle:{ 
                  normal:{ 
                        label:{ 
                          show: true, 
                          formatter: '{b} : {c} ({d}%)' 
                        }, 
                        labelLine :{show:true} 
                      } 
                  } 
        }]
    });
    //单击片区图表打开
    myAreaChart.on('click',function(params){
    	openContractList(2,nowSelectDept,params.name);
    });
 
    //行业分析
    /*myIndustryChart.setOption({
        title: {
            text: '行业分析'
        },
        tooltip: {},
        legend: {
            data:['销售额(元)']
        },
        xAxis: {
            data: []
        },
        yAxis: {},
        itemStyle : {
        	normal: {
        		color: function(params) {
        			var colorList = [
                       '#FF3333','#00c0ef'
                       ];
                    return colorList[params.dataIndex%2]
                },
        	}
        },
        series: [{
            name: '销售额(元)',
            type: 'bar',
            barWidth : 50,
            data: []
        }]
    });*/
   /* myIndustryChart.setOption({
        title: {
            text: '行业分析'
        },
        tooltip: {},
        series : [{
              name: '销售额(元)',
              type: 'pie',
              radius: '55%',
              data:[]
        }]
    });*/
    
    //单击行业图表打开
    myIndustryChart.on('click',function(params){
    	openContractList(3,params.name);
    });
    //单击信用评分图表打开
    myTrendChart.on('click',function(params){
    	openContractList(7,params.name);
    });
    //趋势分析
   /* myTrendChart.setOption({
        title : {
            text: '趋势分析'
        },
        tooltip : {
            trigger: 'axis'
        },
        legend: {
            data:['合同金额']
        },
        toolbox: {
            show : true,
//            feature : {
//                mark : {show: true}
//                //dataView : {show: true, readOnly: false},
//                //magicType : {show: true, type: ['line', 'bar']}
//                //restore : {show: true},
//                //saveAsImage : {show: true}
//            }
        },
        calculable : true,
        xAxis : [
            {
                type : 'category',
                boundaryGap : false,
                data : []
            }
        ],
        yAxis : [
            {
                type : 'value',
                axisLabel : {
                    formatter: '{value}'
                }
            }
        ],
        series : [
            {
                name:'合同金额',
                type:'line',
                data:[],
                markLine : {
                    data : [
                        {type : 'average', name: '平均值'}
                    ]
                }
            }
        ]
    });
    */
    //单击趋势图表打开
    /*myTrendChart.on('click',function(params){
    	openContractList(4,params.name);
    });*/
    
    //合同类型分析
    /*myContractTypeChart.setOption({
        title: {
            text: '合同类型分析'
        },
        tooltip: {},
        series : [{
              name: '销售额',
              type: 'pie',
              radius: '55%',
              data:[]
        }]
    });*/
    
    //单击趋势图表打开
    /*myContractTypeChart.on('click',function(params){
    	openContractList(6,params.name);
    });*/
    
    //初始化加载一次非片区报表
    queryAllChart();
})


//初始化查询条件
function initQueryParam(){
	//$('#endDate').val(new Date().getFullYear()+"-"+((new Date().getMonth() + 1+"").length == 1 ? ("0"+(new Date().getMonth() + 1)) : new Date().getMonth() + 1) +"-"+new Date().getDate());
	//$('#endDate').val(new Date().getFullYear()+"-"+((new Date().getMonth() + 1+"").length == 1 ? ("0"+(new Date().getMonth() + 1)) : new Date().getMonth() + 1) +"-"+((new Date().getDate()+"").length == 1 ? ("0"+new Date().getDate()) : new Date().getDate()));
	$('#endDate').val(getDay(1));
	$('#beginDate').val(new Date().getFullYear()+"-01-01");
}
function getDay(day){  
    var today = new Date();  
      
    var targetday_milliseconds=today.getTime() + 1000*60*60*24*day;          

    today.setTime(targetday_milliseconds); //注意，这行是关键代码    
      
    var tYear = today.getFullYear();  
    var tMonth = today.getMonth();  
    var tDate = today.getDate();  
    tMonth = doHandleMonth(tMonth + 1);  
    tDate = doHandleMonth(tDate);  
    return tYear+"-"+tMonth+"-"+tDate;  
}  
function doHandleMonth(month){  
    var m = month;  
    if(month.toString().length == 1){  
       m = "0" + month;  
    }  
    return m;  
}  

//查询图标非片区 
function queryAllChart(){
	//initQueryParam();
	//片区分析
    myAreaChart.setOption({
        title: {
            text: '片区分析'
        },
        tooltip: {},
        series : [{
              name: '客户数量',
              type: 'pie',
              radius: '55%',
              data:[],
              itemStyle:{ 
                  normal:{ 
                        label:{ 
                          show: true, 
                          formatter: '{b} : {c} ({d}%)' 
                        }, 
                        labelLine :{show:true} 
                      } 
                  } 
        }]
    });
	queryData.beginDate = jQuery('#beginDate').val();
	queryData.endDate = jQuery('#endDate').val();
	//queryData.dateTimeType = jQuery('#dateTimeType').val();
	if(jQuery('#beginDate').val() == ""){
		alert("查询开始时间不能为空!");
		return;
	}
	if(jQuery('#endDate').val() == ""){
		alert("查询结束时间不能为空!");
		return;
	}
	//趋势图
	jQuery.ajax({
		url : encodeURI(urlMyDeptTrend),
		type : 'get',
		data:queryData,
		dataType : 'json',
		contentType : 'application/json;charset=utf-8',
		success : function(data) {
			// 指定图表的配置项和数据
			myTrendDeptChart.setOption({
				xAxis : [
				            {
				                type : 'category',
				                boundaryGap : false,
				                data : data.Axis
				            }
				        ],
		        series : [
		            {
		                name:'销售处客户数量',
		                type:'line',
		                data:data.seriesSumTotal,
		                markLine : {
		                    data : [
		                        {type : 'average', name: '平均值'}
		                    ]
		                }
		            },{
		                name:'成套处客户数量',
		                type:'line',
		                data:data.CTseriesSumTotal,
		                markLine : {
		                    data : [
		                        {type : 'average', name: '平均值'}
		                    ]
		                }
		            },{
		                name:'外贸处客户数量',
		                type:'line',
		                data:data.WMseriesSumTotal,
		                markLine : {
		                    data : [
		                        {type : 'average', name: '平均值'}
		                    ]
		                }
		            }
		        ]
		        
		    });
		}
	});
	//资产性质
	jQuery.ajax({
		url : encodeURI(urlMyZCXZTrend),
		type : 'get',
		data:queryData,
		dataType : 'json',
		contentType : 'application/json;charset=utf-8',
		success : function(data) {
			// 指定图表的配置项和数据
			ZCXZ.setOption({
		        xAxis: {
		            data: data.Axis
		        },
		        series: [{
		            // 根据名字对应到相应的系列
		            name: '客户数量(个)',
		            barWidth : 50,
		            data: data.series
		        }]
		    });
		}
	});
	
	jQuery.ajax({
		url : encodeURI(urlDept),
		type : 'get',
		data:queryData,
		dataType : 'json',
		contentType : 'application/json;charset=utf-8',
		success : function(data) {
			// 指定图表的配置项和数据
			myDeptChart.setOption({
		        xAxis: {
		            data: data.Axis
		        },
		        series: [{
		            // 根据名字对应到相应的系列
		            name: '客户数量(个)',
		            barWidth : 50,
		            data: data.series
		        }]
		    });
		}
	});
	//行业
	jQuery.ajax({
		url : encodeURI(urlIndustry),
		type : 'get',
		data:queryData,
		dataType : 'json',
		contentType : 'application/json;charset=utf-8',
		success : function(data) {
			// 指定图表的配置项和数据
			myIndustryChart.setOption({
				series: [{
		              name: '客户数',
		              type: 'pie',
		              radius: '55%',
		              data:data.data
		        }]
		    });
		}
	});
	//信用评分
	jQuery.ajax({
		url : encodeURI(urlMyTrend),
		type : 'get',
		data:queryData,
		dataType : 'json',
		contentType : 'application/json;charset=utf-8',
		success : function(data) {
			// 指定图表的配置项和数据
			myTrendChart.setOption({
				series: [{
		              name: '客户数',
		              type: 'pie',
		              radius: '55%',
		              data:data.data
		        }]
		    });
		}
	});
	//客户总数
	//queryData.beginDate = "2012-01-01";
	 var mydate = new Date();
	   var str = "" + mydate.getFullYear() + "-";
	   str += (mydate.getMonth()+1) + "-";
	   str += mydate.getDate();
	   //queryData.endDate = str;
	//queryData.beginDate = jQuery("#beginDate").val();
	//queryData.endDate = jQuery("#endDate").val();
	 var urlCount = window.rootPath + '/ui/CustomerTotalInfo/getCustomerCountChart';
	 jQuery.ajax({
			url : encodeURI(urlCount),
			type : 'get',
			data:queryData,
			dataType : 'json',
			contentType : 'application/json;charset=utf-8',
			success : function(data) {
				jQuery("#customerCount").val(data.data);
			}
		});
	 var thisdate = new Date();
	var thisYear =  thisdate.getFullYear();
	queryData.beginDate = thisYear+"-01-01";
	//queryData.beginDate = "2016-01-01";
	//本年新增客户总数
	queryData.endDate = str;
	 jQuery.ajax({
			url : encodeURI(urlCount),
			type : 'get',
			data:queryData,
			dataType : 'json',
			contentType : 'application/json;charset=utf-8',
			success : function(data) {
				jQuery("#thisYearCustomerCount").val(data.data);
			}
		});
	 var urlDeptCount = window.rootPath + '/ui/CustomerTotalInfo/getDeptCustomerCountChart';
	 //销售处本年新增客户
	 queryData.beginDate = thisYear+"-01-01";
	 queryData.endDate = str;
	 queryData.deptName = '销售处';
	 jQuery.ajax({
			url : encodeURI(urlDeptCount),
			type : 'get',
			data:queryData,
			dataType : 'json',
			contentType : 'application/json;charset=utf-8',
			success : function(data) {
				jQuery("#XSCustomerCount").val(data.data);
			}
		});
	 //成套处本年新增客户总数
	 queryData.beginDate = thisYear+"-01-01";
	 queryData.endDate = str;
	 queryData.deptName = '成套处';
	 jQuery.ajax({
			url : encodeURI(urlDeptCount),
			type : 'get',
			data:queryData,
			dataType : 'json',
			contentType : 'application/json;charset=utf-8',
			success : function(data) {
				jQuery("#CTCustomerCount").val(data.data);
			}
		});
	//外贸处本年新增客户总数
	 queryData.beginDate = thisYear+"-01-01";
	 queryData.endDate = str;
	 queryData.deptName = '外贸处';
	 jQuery.ajax({
			url : encodeURI(urlDeptCount),
			type : 'get',
			data:queryData,
			dataType : 'json',
			contentType : 'application/json;charset=utf-8',
			success : function(data) {
				jQuery("#WMCustomerCount").val(data.data);
			}
		});
	 
}
	
//查询类型 1：部门 2：片区 3：行业 4：趋势 5：产品类型 6：合同类型
function openContractList(type,value,value1){
	var url = window.rootPath + "/ui/CustomerTotalIndex";
	if(type == 1){
		url += "?deptName=" +value;
	}else if(type == 2){
		url += "?deptName=" +value + "&areaName="+value1;
	}else if(type == 3){
		url += "?industryName=" +value;
	}else if(type == 7){
		/*var beginDate = value.split("年")[0] + "-" + ((value.split("年")[1].split("月")[0] < 10) ? "0"+value.split("年")[1].split("月")[0] : value.split("年")[1].split("月")[0]) + "-01";
		var endDate = value.split("年")[0] + "-" + (((parseInt(value.split("年")[1].split("月")[0])+1) < 10) ? "0"+(parseInt(value.split("年")[1].split("月")[0])+1) : (parseInt(value.split("年")[1].split("月")[0])+1)) + "-01";
		if(value.split("年")[1].split("月")[0] == 12){//要进一个月
			endDate = parseInt(value.split("年")[0])+1 + "-01-01";
		}
		url += "?beginDate=" +beginDate + "&endDate="+endDate;*/
		url += "?xyName=" +value;
	}else if(type == 5){
		
	}else if(type == 6){
		url += "?contractType=" +value;
	}else if(type == 8){
		var beginDate = value.split("年")[0] + "-" + ((value.split("年")[1].split("月")[0] < 10) ? "0"+value.split("年")[1].split("月")[0] : value.split("年")[1].split("月")[0]) + "-01";
		var endDate = value.split("年")[0] + "-" + (((parseInt(value.split("年")[1].split("月")[0])+1) < 10) ? "0"+(parseInt(value.split("年")[1].split("月")[0])+1) : (parseInt(value.split("年")[1].split("月")[0])+1)) + "-01";
		if(value.split("年")[1].split("月")[0] == 12){//要进一个月
			endDate = parseInt(value.split("年")[0])+1 + "-01-01";
		}
		url += "?beginDate=" +beginDate + "&endDate="+endDate;
	}else if(type == 9){
		url += "?Asset=" +value;
	}
	
	if(type != 4){
		url += "&beginDate=" + jQuery('#beginDate').val() + "&endDate=" + jQuery('#endDate').val();
	}
	url += "&dateTimeType=" + jQuery('#dateTimeType').val();
	window.open(url);
}